package uiLayer;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.swing.AbstractCellEditor;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;

import modelLayer.Class;
import modelLayer.ClassRoom;
import modelLayer.Subject;
import modelLayer.Teacher;
import modelLayer.TeachingBlock;
import uiLayer.util.EditViewTemplate;
import utilLayer.AlreadyExistsException;

import javax.swing.border.BevelBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellEditor;

import controlLayer.ClassRoomCtr;
import controlLayer.SubjectCtr;
import controlLayer.TeachingBlockCtr;

import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;

public class TeachingBlockEditPanel extends EditViewTemplate {
	private static final long serialVersionUID = 1L;
	
	private TeachingBlockCtr tbCtr;
	private Class cl;
	private TeachingBlockTable tbTable;
	private List<Teacher> teacherList;
	private List<Subject> subjectList;
	private List<ClassRoom> classRoomList;
	private List<TeachingBlock> tbList;
	private DefaultComboBoxModel<Teacher> cBoxTeacherList;
	private DefaultComboBoxModel<Subject> cBoxSubjectList1;
	private DefaultComboBoxModel<Subject> cBoxSubjectList2;
	private DefaultComboBoxModel<ClassRoom> cBoxClassRoomList;
	private JComboBox<Subject> cBoxSubject1;
	private JComboBox<ClassRoom> cBoxClassRoom;
	private JComboBox<Teacher> cBoxTeacher;
	private JComboBox<Subject> cBoxSubject2;
	private JSpinner noPerWeek;
	
		 
	public TeachingBlockEditPanel(Class c){
		this.cl = c;
		try {
			tbCtr = new TeachingBlockCtr();
		}
		catch (Exception e) {
			
		}
		super.getBtnReset().setText("Slet alt");
		
		initiateClassRoomList();
		initiateSubjectList();
		
		JPanel tbCenterPanel = new JPanel(new BorderLayout());
		getCenterPanel().add(tbCenterPanel, BorderLayout.CENTER);
		tbCenterPanel.setBorder(new TitledBorder(new EmptyBorder(5, 5, 5, 5), "Tilf\u00F8j fag til pensum", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		
		JPanel addSubjectPanel = new JPanel();
		addSubjectPanel.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
		tbCenterPanel.add(addSubjectPanel, BorderLayout.NORTH);
		addSubjectPanel.setLayout(new BorderLayout(0, 0));
		
		JPanel selectionPanel = new JPanel();
		selectionPanel.setBorder(new EmptyBorder(10, 10, 10, 10));
		addSubjectPanel.add(selectionPanel, BorderLayout.CENTER);
		selectionPanel.setLayout(new GridLayout(0, 4, 5, 5));
		
		JLabel lblSubject = new JLabel("Fag:");
		selectionPanel.add(lblSubject);
		
		JLabel lblTeacher = new JLabel("Lærer:");
		selectionPanel.add(lblTeacher);
		
		JLabel lblClassRoom = new JLabel("Lokale:");
		selectionPanel.add(lblClassRoom);
		
		JLabel lblNumberPerWeek = new JLabel("Antal pr. uge:");
		selectionPanel.add(lblNumberPerWeek);
		
		
		cBoxSubject1 = new JComboBox<>();
		cBoxSubjectList1 = new DefaultComboBoxModel<Subject>();
		cBoxSubject1.setModel(cBoxSubjectList1);
		cBoxSubject1.setMaximumRowCount(5);
		cBoxSubject1.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				doSetTeacherList();
				//Subject s = (Subject) cBoxSubject1.getSelectedItem();
				//txtTeacher.setText(s.getTeacher().getName());
			}
		});
		selectionPanel.add(cBoxSubject1);
		
		cBoxTeacher = new JComboBox<>();
		cBoxTeacherList = new DefaultComboBoxModel<Teacher>();
		//initiateCBoxTeacher();
		cBoxTeacher.setModel(cBoxTeacherList);
		selectionPanel.add(cBoxTeacher);
		
		cBoxClassRoom = new JComboBox<>();
		cBoxClassRoomList = new DefaultComboBoxModel<ClassRoom>();
		initiateCBoxClassRoom();
		cBoxClassRoom.setModel(cBoxClassRoomList);
		cBoxClassRoom.setMaximumRowCount(5);
		selectionPanel.add(cBoxClassRoom);
		
		noPerWeek = new JSpinner();
		noPerWeek.setModel(new SpinnerNumberModel(1, 1, 35, 1));
		selectionPanel.add(noPerWeek);
		
		JPanel buttonPanel = new JPanel();
		FlowLayout flowLayout = (FlowLayout) buttonPanel.getLayout();
		flowLayout.setAlignment(FlowLayout.TRAILING);
		addSubjectPanel.add(buttonPanel, BorderLayout.SOUTH);
		
		JButton btnOk1 = new JButton("OK");
		btnOk1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				createTeachingBlock();
			}
		});
		buttonPanel.add(btnOk1);
		
		JPanel editSubjectPanel = new JPanel();
		tbCenterPanel.add(editSubjectPanel, BorderLayout.CENTER);
		editSubjectPanel.setBorder(new TitledBorder(new EmptyBorder(5, 5, 5, 5), "Rediger klassens pensum", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		editSubjectPanel.setLayout(new BorderLayout(0, 0));
		
		JScrollPane tableScrollPane = new JScrollPane();
		tbTable = new TeachingBlockTable();
		initiateTable();
		tbTable.setDefaultRenderer(ClassRoom.class, new ClassRoomCellRenderer());
		tbTable.setDefaultEditor(ClassRoom.class, new ClassRoomCellEditor(classRoomList));
		tableScrollPane.setViewportView(tbTable);
		editSubjectPanel.add(tableScrollPane, BorderLayout.CENTER);
		
		JPanel deleteSubjectPanel = new JPanel();
		tbCenterPanel.add(deleteSubjectPanel, BorderLayout.SOUTH);
		deleteSubjectPanel.setBorder(new TitledBorder(new EmptyBorder(5, 5, 5, 5), "Fjern fag fra pensum", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		deleteSubjectPanel.setLayout(new BorderLayout(0, 0));
		
		JPanel deleteSubPanel = new JPanel();
		deleteSubPanel.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
		deleteSubjectPanel.add(deleteSubPanel, BorderLayout.CENTER);
		deleteSubPanel.setLayout(new FlowLayout(FlowLayout.LEADING, 5, 5));
		
		cBoxSubject2 = new JComboBox<>();
		cBoxSubjectList2 = new DefaultComboBoxModel<Subject>();
		cBoxSubject2.setModel(cBoxSubjectList2);
		initiateCBoxSubject();
		cBoxSubject2.setMaximumRowCount(5);
		deleteSubPanel.add(cBoxSubject2);
		
		JButton btnDeleteSubject = new JButton("Slet");
		btnDeleteSubject.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				deleteSubject();
			}
		});
		deleteSubPanel.add(btnDeleteSubject);
		
	}

	
	private void initiateSubjectList() {
		List <Subject> subjectList = null;
		try {
			//Map<String, Subject> subjects = new SubjectCtr().getAll(true);
			//subjectList = new ArrayList<>();
			subjectList = new SubjectCtr().getAll(true);
//			for (Subject s : subjects.values()){
//				subjectList.add(s);
//			}
			if (subjectList.size() > 0) {
				this.subjectList = subjectList;
			}
			else {
				new StandardDialog().objectNotFoundDialog(" List af fag");
			}
		}
		catch (Exception e) {
			//db connection error
		}
		
	}

	protected void doSetTeacherList() {
		Subject s = (Subject) cBoxSubject1.getSelectedItem();
		cBoxTeacherList.removeAllElements();
		initiateCBoxTeacher(s);
		
	}
	
	private void initiateCBoxTeacher(Subject s) {
		if (s != null){
			teacherList = s.getTeachers();
			System.out.println(teacherList);
		}
		if (teacherList.size() > 0){
			for (Teacher t : teacherList){
				cBoxTeacherList.addElement(t);
			}
		}
	}

	private void initiateCBoxClassRoom() {
		if (classRoomList.size() > 0){
			for (ClassRoom cr : classRoomList) {
				cBoxClassRoomList.addElement(cr);
			}
		}
	}

	private void initiateCBoxSubject() {
		try {
			System.out.println(subjectList.size());
			if (subjectList.size() > 0) {
				for (Subject s : subjectList) {
					cBoxSubjectList1.addElement(s);
					cBoxSubjectList2.addElement(s);
				}
			}
			else {
				new StandardDialog().objectNotFoundDialog("fag ");
			}
		}
		catch (Exception e) {
			//db connection error
		}
	}


	private void initiateClassRoomList() {
		List <ClassRoom> classRoomList = null;
		try {
			classRoomList = new ClassRoomCtr().getAll();
			if (classRoomList != null) {
				this.classRoomList = classRoomList;
			}
			else {
				new StandardDialog().objectNotFoundDialog(" List af lokaler");
			}
		}
		catch (Exception e) {
			//db connection error
		}
		
	}


	private void initiateTable() {
		this.tbList = null;
		try {
			this.tbList = tbCtr.getAllByClass(cl.getId(), true);
			if (tbList != null){
				tbTable.setModel(tbList);
			}
		}
		catch (Exception e) {
			//db connection error
		}
	}
	
	protected void createTeachingBlock() {
		Subject s = (Subject) cBoxSubject1.getSelectedItem();
		ClassRoom cr = (ClassRoom) cBoxClassRoom.getSelectedItem();
		Teacher t = (Teacher) cBoxTeacher.getSelectedItem(); 
		int number = (int) noPerWeek.getValue(); 
		try {
			if (cl != null) {
			tbCtr.createTeachingBlock(cl.getId(), s.getId(), cr.getId(), t.getId(), number);
			initiateTable();
			noPerWeek.setValue(1);
			}
			else {
				new StandardDialog().noSelectionDialog("klasse ");
			}
		}
		catch (AlreadyExistsException e) {
			new StandardDialog().showErrorMessage(e.getMessage());
		}
		catch (Exception e) {
			//TODO check it up
		}
	}

	
	protected void deleteSubject() {
		Subject s = (Subject) cBoxSubject2.getSelectedItem();
		
		if  (cl != null) {
			
			int n = new StandardDialog().deleteDialog("fag", s.getName());
			if (n == JOptionPane.YES_OPTION){
				tbCtr.removeByClassAndSubject(cl.getId(), s.getId());
				initiateTable();
			}
			
		}
		else {
			new StandardDialog().noSelectionDialog("klasse ");
		}
	}
		
	//Delete all
	@Override
	protected void doReset() {
		List<TeachingBlock> tbList = tbTable.getDataModel();
		
		if(tbList != null) {
			int n = new StandardDialog().deleteAllDialog("fag", this.cl.getClassName());
			if(n == JOptionPane.YES_OPTION) {
				for(TeachingBlock tb : tbList) {
					//tbCtr.removeByClassAndSubject(id, tb.getSubject().getId());
					tbCtr.removeTeachingBlock(tb);
				}
				initiateTable();
			}
		}
	}

	

	@Override
	protected void doSave() {
		List<TeachingBlock> teachingBlocks = tbTable.getDataModel();
		if (!teachingBlocks.isEmpty()){
			System.out.println(teachingBlocks);
			try {
				for (TeachingBlock tb : teachingBlocks){
					this.tbCtr.updateTeachingBlock(tb);
					
				}
				initiateTable();
				new StandardDialog().objectSaved("Listen af undervisningblokke for " + cl);
			}
			catch (Exception e) {
				//
			}
		}
	}
	

	
	//////////////////////////Inner Class
	private class ClassRoomCellRenderer extends DefaultTableCellRenderer {

		private static final long serialVersionUID = 1L;

		public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
					boolean hasFocus, int row, int column) {
			if (value instanceof ClassRoom) {
				ClassRoom classRoom = (ClassRoom) value;
				setText(classRoom.getName());
			}
//			if (isSelected) {
//				setBackground(table.getSelectionBackground());
//			}
//			else {
//				setBackground(table.getSelectionForeground());
//			}
			return this;
		}
	}
	
	//////////////////////////Inner Class
	private class ClassRoomCellEditor extends AbstractCellEditor implements
	TableCellEditor, ActionListener {

	private static final long serialVersionUID = 1L;
	private ClassRoom classRoom;
	private List<ClassRoom> classRoomList;
	
	public ClassRoomCellEditor(List<ClassRoom> classRoomList){
		this.classRoomList = classRoomList;
	}
	
	@Override
	public Object getCellEditorValue() {
		return this.classRoom;
	}
	
	@Override
	public void actionPerformed(ActionEvent event) {
		JComboBox<ClassRoom> comboBoxClassRoom = (JComboBox<ClassRoom>) event.getSource();
		this.classRoom = (ClassRoom) comboBoxClassRoom.getSelectedItem();

	}

	@Override
	public Component getTableCellEditorComponent(JTable table, Object value,
			boolean isSelected, int row, int column) {
		if (value instanceof ClassRoom){
			this.classRoom = (ClassRoom) value;
		}
		JComboBox<ClassRoom> comboBoxClassRoom = new JComboBox<ClassRoom>();
		
		for (ClassRoom cr : classRoomList) {
			comboBoxClassRoom.addItem(cr);
		}
		
		comboBoxClassRoom.setSelectedItem(classRoom);
		comboBoxClassRoom.addActionListener(this);
		
	//	if (isSelected) {
	//		comboBoxClassRoom.setBackground(table.getSelectionBackground());
	//	}
	//	else {
	//		comboBoxClassRoom.setBackground(table.getSelectionForeground());
	//	}
		return comboBoxClassRoom;
	}

}

}
